# -*- coding: utf-8 -*-
from pub.models import Cuisine, Bus_area, District, Dict
import json

class CuisinesEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Cuisine):
            if( obj.parent ):
                return {"id":obj.id, "name":obj.name, "level":obj.level,"parent_id":obj.parent.id}
            else:
                return {"id":obj.id, "name":obj.name, "level":obj.level,"parent_id":obj.parent}
        return json.JSONEncoder.default(self, obj)
def pubMess(request):
    
    #def get_cuisines():
    #    c = json.dumps(list(Cuisine.objects.all()), cls=CuisinesEncoder)
    #    return c
    def get_cuisines():
        '''
            cuisines = Cuisine.objects.filter(level='1')
            str = ""
            for cuisine in cuisines:
                str = str+"<li rel='%s'>%s"  % (cuisine.id,cuisine.name)
                child_cuisines = Cuisine.objects.filter(parent = cuisine.id)
                
                if child_cuisines:
                    str = str + "<ul>"
                    for child_cuisine in child_cuisines:
                        str = str + "<li rel='%s'>%s"  % (child_cuisine.id,child_cuisine.name)
                        str = str + "</li>"
                    str = str + "</ul>"    
                str = str + "</li>"    
            return str
        '''
        cuisines = Cuisine.objects.all()
        return cuisines
    def get_hot_cuis():
        cuisines = Cuisine.objects.filter(level='1').order_by("-weight")[0:5]
        return cuisines
    
    def get_bus_areas():
        return Bus_area.objects.all()
    
    def get_hot_bus_areas():
        areas = Bus_area.objects.filter(district__name="东莞").order_by("-weight")[0:5]
        return areas 
    def get_spend():
        spend = Dict.objects.filter(type="spend").order_by("-weight") 
        return spend
    def get_district():
        districts = District.objects.filter(level='1')
        str = ""
        for district in districts:
            str = str+"<li rel='%s'>%s"  % (district.id,district.name)
            two_districts = District.objects.filter(parent = district.id)
            
            if two_districts:
                str = str + "<ul>"
                for two_district in two_districts:
                    str = str + "<li rel='%s'>%s"  % (two_district.id,two_district.name)
                    
                    three_districts = District.objects.filter(parent = two_district.id)
            
                    if three_districts:
                        str = str + "<ul>"
                        for three_district in three_districts:
                            str = str + "<li rel='%s'>%s"  % (three_district.id,three_district.name)
                            str = str + "</li>"
                        str = str + "</ul>"
                    
                    str = str + "</li>"
                str = str + "</ul>"    
            str = str + "</li>"    
        return str
    def get_provinces():
        pass
    def get_citys():
        pass
    def get_districts():
        pass    

    return {
        'cuisines':get_cuisines,
        #热门菜系
        'hot_cuis':get_hot_cuis,
        #热门地标
        'hot_bus_areas':get_hot_bus_areas,
        #热门地标
        'spend':get_spend,
        'bus_area': get_bus_areas,
        'district': get_district,
    }
